package com.avertymaroc.library;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.avertymaroc.app.R;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public abstract class HttpManager extends AsyncTask<String, String, String> {

	private String url;
	private Context context = null;
	private ProgressDialog pDialog = null; 
    private JSONParser jsonParser = null;
    private String msgDialog;
    private List<NameValuePair> params = null; 
    private String methode = "POST";
    private  JSONObject resultatsJSON;
    ConnectionDetector connexionDetector ;

   
    
    public HttpManager(Context context) {
    		this.context = context;
    		jsonParser = new JSONParser();
    		msgDialog = "Chargement. Attendez SVP...";
    		params = new ArrayList<NameValuePair>();
    		
    		pDialog = new ProgressDialog(this.context);
    		pDialog.setMessage(this.msgDialog);
    		pDialog.setIndeterminate(false);
    		pDialog.setCancelable(true);
    		connexionDetector = new ConnectionDetector(context);
	}
    
    public HttpManager(Context context ,String url  ,Map<String, String> params){
    	jsonParser = new JSONParser();
		msgDialog = "Chargement. Attendez SVP...";
		
		this.setParams(params);
		this.url = url;
		this.context = context;
		
		pDialog = new ProgressDialog(this.context);
		pDialog.setMessage(this.msgDialog);
		pDialog.setIndeterminate(false);
		pDialog.setCancelable(false);
		connexionDetector = new ConnectionDetector(context);
    	
    }
    
    @Override
    protected void onPreExecute() {
    	
    	if(connexionDetector.isConnectingToInternet()){
        super.onPreExecute();
        if(pDialog != null)
        pDialog.show();
    	}else{
    		this.cancel(true);
    		AlertDialogue.afficher(context, R.string.no_connexion_detected);
    	}
        
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
    	
    	resultatsJSON = jsonParser.makeHttpRequest(this.url, this.methode, this.params);
        Log.d("Resultats : ", resultatsJSON.toString());
        return null;
    }
 
    protected abstract void onPostExecute(String file_url);
    
    
	public String getUrl() {
		return url;
	}
	public HttpManager setUrl(String url) {
		this.url = url;
		return this;
	}
	
	public Context getContext() {
		return context;
	}
	public HttpManager setContext(Context context) {
		this.context = context;
		return this;
	}
	
	public ProgressDialog getpDialog() {
		return pDialog;
	}
	public HttpManager setpDialog(ProgressDialog pDialog) {
		this.pDialog = pDialog;
		return this;
	}
	
	
	public String getMsgDialog() {
		return msgDialog;
	}
	public HttpManager setMsgDialog(String msgDialog) {
		this.msgDialog = msgDialog;
		return this;
	}
	
	public List<NameValuePair> getParams() {
		return params;
	}
	public HttpManager setParams(Map<String, String> params) {
	for(Entry<String, String> entry : params.entrySet())
				this.params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));	
	return this;
	}
	
	public String getMethode() {
		return methode;
	}
	public HttpManager setMethode(String methode) {
		this.methode = methode;
		return this;
	} 
	public  JSONObject getResult(){
		
		return resultatsJSON;
	}
   
    
    

}

